package com.tt.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tt.reggie.common.BaseContext;
import com.tt.reggie.common.R;
import com.tt.reggie.entity.AddressBook;
import com.tt.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list();
        if(list==null){
            addressBook.setIsDefault(1);
        }
        addressBookService.save(addressBook);
        return R.success("保存成功！");
    }


    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(userId!=null,AddressBook::getUserId,userId);
        lambdaQueryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(lambdaQueryWrapper);
        return R.success(list);
    }


    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(wrapper);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功！");
    }

    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable long id){
        AddressBook byId = addressBookService.getById(id);
        if(byId==null){
            return R.error("找不到该地址");
        }
        return R.success(byId);
    }

    @PutMapping
    public R<String> updateById(@RequestBody AddressBook addressBook){
        if(addressBook!=null){
            addressBookService.updateById(addressBook);
            return R.success("修改成功！");
        }
        return R.success("修改失败");

    }

    @DeleteMapping
    public R<String> deleteById(@RequestParam Long ids){
        if(ids!=null){
            addressBookService.removeById(ids);
            return R.success("删除成功！");
        }
        return R.error("删除失败!");
    }


    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault,1).eq(AddressBook::getUserId,userId);
        AddressBook one = addressBookService.getOne(wrapper);
        if(one==null){
            return R.error("该用户还没有设置地址哦");
        }
        return R.success(one);
    }


}
